//
// Created by shao.liming on 2/2/2018.
//

#include "E248.h"

int getValue(char c){
    if(c >= '0' && c <= '9')
        return (c - '0');
    else
        return -1;
}

int E248::convert(std::string str) {
    if (str.empty())
        return 0;

    int ret = 0;

    bool x = false;
    if (str[0] == '-'){
        str.erase(0, 1);
        x = true;
    }

    if (str[0] == '0')
        return 0;

    for (char i : str) {
        printf("%c ", i);
        int v = getValue(i);
        if (v == -1){
            ret = 0;
            break;
        }

        ret = ret * 10 + v;
        if (ret < 0){
            ret = 0;
            break;
        }
    }

    if (x)
        ret *= -1;

    return ret;
}

void E248::test() {
    std::string str = "-2147483647";
    int sum = convert(str);
    printf("\nValue = %d", sum);
}
